<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author XPF
-->
<template>
  <div>
    <BasicModal
      v-bind="$attrs"
      width="1000px"
      :wrapperFooterOffset="180"
      :centered="true"
      @register="registerModal"
      @ok="handleSubmit"
    >
      <template #title>
        <span> {{ getTitle.value }} </span>
      </template>
      <BasicTable
        @register="registerForm"
        @success="handleSuccess"
        :pagination="{ pageSize: 10 }"
      />
    </BasicModal>
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewSportsSportsWeightAdditionalModal',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { FormProps } from '/@/components/Form';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import {
    SportsSingleIndex,
    sportsSingleIndexListData,
    saveSingleIndex,
  } from '/@/api/sports/sportsSingleIndex';
  const { showMessage } = useMessage();
  const emit = defineEmits(['success', 'register']);
  const { t } = useI18n('sports.sportsWeight');
  const record = ref<SportsSingleIndex>({} as SportsSingleIndex);
  const getTitle = computed(() => ({
    value: t('待选评分规则'),
  }));
  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('状态'),
        field: 'status',
        component: 'Select',
        componentProps: {
          dictType: 'sys_search_status',
          allowClear: true,
          onChange: handleSuccess,
        },
      },
    ],
  };
  const tableColumns: BasicColumn[] = [
    {
      title: t('指标名称'),
      dataIndex: 'sportsWeight.indexName',
      key: 's_index_name',
      //sorter: true,
      align: 'left',
      dictType: 's_index_name',
      slots: { customRender: 'firstColumn' },
    },
    {
      title: t('适配年级'),
      dataIndex: 'scoreAdaptGrade',
      key: 's_score_adapt_grade',
      //sorter: true,
      align: 'center',
      dictType: 'vke_grade',
    },
    {
      title: t('适配性别'),
      dataIndex: 'scoreAdaptSex',
      key: 'a.score_adapt_ex',
      //sorter: true,
      align: 'left',
      dictType: 'sys_user_sex',
    },
    // {
    //   title: t('符号'),
    //   dataIndex: 'scoreSymbol',
    //   key: 's.score_sumbol',
    //  // sorter: true,
    //   align: 'left',
    //   dictType: 's_scoring_rules',
    // },
    {
      title: t('分值'),
      dataIndex: 'score1',
      key: 'a.adapt_sex',
     // sorter: true,
      align: 'left',
    },
  ];
  const [
    registerForm,
    { reload, getSelectRows, setTableData, clearSelectedRowKeys, setSelectedRowKeys },
  ] = useTable({
    api: async (params) => {
      const res = await sportsSingleIndexListData({
        indexId: dataTable.record.indexId,
        pageNo: params.pageNo,
        pageSize: params.pageSize,
      });
      res.list.forEach((item) => {
        if (item.score1 != '' && item.score2 != '') {
          item.score1 = item.score1 + ' - ' + item.score2;
        } else if (item.score1 == '' || item.score2 == '') {
          item.score1 = item.score1 + item.score2;
        }
      });
      return res;
    },
    beforeFetch: (params) => {
      params.pageSize = 10;
      console.log(params);
      return params;
    },
    columns: tableColumns,
    pagination: true,
    clickToRowSelect: true,
    rowSelection: { type: 'checkbox' },
    canResize: true,
    showTableSetting: true,
  });

  let dataTable;
  const [registerModal, { closeModal, setModalProps }] = useModalInner(async (data) => {
    reload();
    setModalProps({ loading: true });
    setSelectedRowKeys(data.keyArr);
    dataTable = data;
    // const res = await sportsSingleIndexListData({ indexId: data.indexId });
    // setTableData(res);
    setModalProps({ loading: false });
  });
  async function handleSubmit() {
    try {
      let data = getSelectRows();
      let arr = [];
      let dataId = {
        scoreId: '',
        scoreGradeId: dataTable.record.scoreGradeId,
      };
      data.forEach((item) => {
        arr.push(item.scoreId);
      });
      dataId.scoreId = String(arr);
      let res = await saveSingleIndex(dataId);
      showMessage(res.message);
      clearSelectedRowKeys();
      setTimeout(closeModal);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(t('您填写的信息有误，请根据提示修正。'));
      }
      console.log('error', error);
    }
  }
  function onSelectChange(selectedRowKeys) {
    selectedRowKeys = selectedRowKeys;
  }
  function handleSuccess() {}
</script>
